[% setvar title New pragma 'autoload' to load functions and modules on-demand %]
<div id="archive-notice">
    <h3>This file is part of the Perl 6 Archive</h3>
    <p>To see what is currently happening visit <a href="http://www.perl6.org/">http://www.perl6.org/</a></p>
</div>
<div class='pod'>
<a name='TITLE'></a><h1>TITLE</h1>
<p>New pragma 'autoload' to load functions and modules on-demand</p>
<a name='VERSION'></a><h1>VERSION</h1>
<pre>  Maintainer: Nathan Wiger &lt;<a href='mailto:nate@wiger.org'>nate@wiger.org</a>&gt;
  Date: 24 Aug 2000
  Last Modified: 19 Sep 2000
  Mailing List: <a href='mailto:perl6-language-subs@perl.org'>perl6-language-subs@perl.org</a>
  Number: 153
  Version: 2
  Status: Frozen</pre>
<a name='ABSTRACT'></a><h1>ABSTRACT</h1>
<p>Some goals for Perl 6 include making it more compact and lighter weight.
While much stuff will remain available, many things are potentially
being moved out of core and into modules, like <code>Socket</code> functions,
<code>Format</code> stuff, <code>chop</code>, <code>localtime</code>, <code>gmtime</code>, and so on.</p>
<p>While good from a computer standpoint, having to start a script with:</p>
<pre>   #!perl -w
   use User::pwent;
   use Time::Local;
   use Format;
   use String::Util;</pre>
<p>Just to parse the <code>/etc/passwd</code> file and print the date is &quot;bad&quot;.</p>
<p>This RFC proposes a new pragma, <code>autoload</code>, that could autoload the
functions from the appropriate modules on-demand. Unlike the existing
<code>autouse</code>, which loads modules on demand that you specifically tell it
to, <code>autoload</code> looks at a configuration file and loads the appropriate
modules for you.</p>
<a name='DESCRIPTION'></a><h1>DESCRIPTION</h1>
<a name='The Basics'></a><h2>The Basics</h2>
<p>The new <code>autoload</code> pragma would rely on an <code>autoload.conf</code> file that
was located in <code>@INC</code> and looked something like this:</p>
<pre>   # proposed autoload.conf format
   # function     module
   getpwnam       User::pwent
   date           Date::Time
   time           Date::Time
   open           IO::File
   chomp          String::Util</pre>
<p>This file could be auto-appended to by module installations, similar to
<code>.packlist</code>. However, it could also be manually modified by the system
administrator. A custom one could also be included in <code>@INC</code> ahead of
the system one so that a user could include their own definitions. The
user can also specify the path to a custom configuration file via the
<code>use autoload</code> pragma directly:</p>
<p>The script would then start with:</p>
<pre>   #!perl -w
   use autoload '/home/nwiger/perl/autoload.conf';
   $uid = getpwnam($&gt;);</pre>
<p>Which would autoload <code>getpwnam</code> from <code>User::pwent</code> per the
<code>/home/nwiger/perl/autoload.conf</code> file. Like <code>use lib</code>, this should
just add an additional config file to read onto the start of the search
path.</p>
<p>In addition, <code>autoload</code> should be able to handle simple <code>use</code>
operations:</p>
<pre>   #!perl -w
   use autoload;
   my $r = new CGI;                 # these are both loaded from
   tie %session, Apache::Session;   # @INC just like via 'use'</pre>
<p>This makes easy things easier. The person still gets the same
functionality as if they had manually <code>use</code>'d (actually,
<code>require</code>'d/<code>import</code>'ed) each module, since <code>@INC</code> is still
searched.</p>
<a name='Module Installation'></a><h2>Module Installation</h2>
<p>On installation, some part of the Perl module install driven by
<code>MakeMaker</code> would have to read the exported function list from a given
module and append it to the <code>autoload.conf</code> file.</p>
<a name='Extensions'></a><h2>Extensions</h2>
<p>The <code>autoload</code> pragma could be extended in several ways:</p>
<pre>   1. If duplicate modules or methods are found, &lt;autoload&gt; should
      issue messages like:

   Duplicate CGI modules found - autoloading /path/to/CGI.pm
   Duplicate getpwnam functions registered - using User::pwent::getpwnam

      C&lt;autoload&gt; should always use the last one in the list,
      since this will be the most recent one if the person
      is not actively maintaining the file (i.e., just letting
      it be appended to automatically).

   2. The C&lt;autoload.conf&gt; file could list version numbers and
      the such to take advantage of advanced module versioning
      as mentioned in RFC 78.</pre>
<a name='IMPLEMENTATION'></a><h1>IMPLEMENTATION</h1>
<p>Mostly harmless. Right before raising the famous &quot;Can't locate method
...&quot; error, Perl should check to see if <code>autoload</code> is in effect. If so,
it should read the <code>autoload.conf</code> config file and try to load the
appropriate function and module. If not possible, then the error should
be raised.</p>
<a name='MIGRATION'></a><h1>MIGRATION</h1>
<p>None. This introduces new functionality.</p>
<a name='REFERENCES'></a><h1>REFERENCES</h1>
<p><a href='http://www.mail-archive.com/perl6-language@perl.org/msg02576.html' target='_blank'>www.mail-archive.com</a></p>
<p>RFC 78: Improved Module Versioning And Searching</p>
<p>Existing <code>use autouse</code> pragma/module</p>
</div>
